package com.marquisx.common.ui.preference;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.marquisx.common.ui.dialog.NumberKeypad;
import com.marquisx.tzdice.Constants;
import com.marquisx.tzdice.R;
import com.marquisx.tzdice.Setting;
import com.marquisx.tzdice.UserProfilePicGrid;

public class NumberPreference extends Preference
{
	private int max;
	private int min;
	private int number;
	private TextView numberTV;
	
	private NumberKeypad numKeypad;
	
	// This is the constructor called by the inflater
	public NumberPreference(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		setWidgetLayoutResource(R.layout.preference_widget_number);
		numKeypad = new NumberKeypad(context);
	}
	
	public void setMax(int max)
	{
		this.max = max;
	}
	
	public void setMin(int min)
	{
		this.min = min;
	}
	
	@Override
	protected void onBindView(View view)
	{
		super.onBindView(view);

		// Set our custom views inside the layout
		numberTV = (TextView) view.findViewById(R.id.preference_number_widget_tv);
		numberTV.setText(number+"");
	}

	@Override
	protected void onClick()
	{
		numKeypad.setMax(max);
		numKeypad.setMin(min);
		
		numKeypad.setButton(R.string.label_ok, new OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				number = numKeypad.getResult();
				persistInt(number);
				notifyChanged();
			}
		});
		
		numKeypad.setButton2(R.string.label_cancel, new OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				//numKeypad.hide();
			}
		});
		
		numKeypad.show(number);
	}

	@Override
	protected Object onGetDefaultValue(TypedArray a, int index)
	{
		// This preference type's value type is Integer, so we read the default
		// value from the attributes as an Integer.
		return a.getInteger(index, 0);
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
	{
		if (restoreValue)
		{
			// Restore state
			number = getPersistedInt(0);
		}
		else
		{
			// Set state
			int value = (Integer) defaultValue;
			number = value;
			persistInt(value);
		}
	}

	@Override
	protected Parcelable onSaveInstanceState()
	{
		/*
		 * Suppose a client uses this preference type without persisting. We must save the instance state so it is able
		 * to, for example, survive orientation changes.
		 */

		final Parcelable superState = super.onSaveInstanceState();
		if (isPersistent())
		{
			// No need to save instance state since it's persistent
			return superState;
		}

		// Save the instance state
		final SavedState myState = new SavedState(superState);
		myState.number = number;
		return myState;
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state)
	{
		if (!state.getClass().equals(SavedState.class))
		{
			// Didn't save state for us in onSaveInstanceState
			super.onRestoreInstanceState(state);
			return;
		}

		// Restore the instance state
		SavedState myState = (SavedState) state;
		super.onRestoreInstanceState(myState.getSuperState());
		number = myState.number;
		notifyChanged();
	}

	/**
	 * SavedState, a subclass of {@link BaseSavedState}, will store the state of MyPreference, a subclass of Preference.
	 * <p>
	 * It is important to always call through to super methods.
	 */
	private static class SavedState extends BaseSavedState
	{
		int number;

		public SavedState(Parcel source)
		{
			super(source);

			// Restore the click counter
			number = source.readInt();
		}

		@Override
		public void writeToParcel(Parcel dest, int flags)
		{
			super.writeToParcel(dest, flags);

			// Save the click counter
			dest.writeInt(number);
		}

		public SavedState(Parcelable superState)
		{
			super(superState);
		}

		public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>()
		{
			public SavedState createFromParcel(Parcel in)
			{
				return new SavedState(in);
			}

			public SavedState[] newArray(int size)
			{
				return new SavedState[size];
			}
		};
	}

}
